package com.jf.springboot.web;

import com.jf.springboot.bean.User;
import com.jf.springboot.utils.FileUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-12-06
 * Time:10:10
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @PostMapping("/login")
    public String login(User user, HttpSession session) {
        if (user.InfoComplete()) {
            session.setAttribute("user", user);
            return "success";
        }
        return "fail";
    }
    @GetMapping("/list")
    public List<User> list() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"jack","abc",10));
        userList.add(new User(2,"tom","abc",20));
        userList.add(new User(3,"kay","abc",30));
        return userList;
    }
    @PostMapping("/upload")
    public String upload(@RequestParam("portrait") MultipartFile portrait,
                         @RequestParam("pets") MultipartFile[] pets) throws IOException {
        System.out.println(portrait.getOriginalFilename() + " " + portrait.getName() + " " + portrait.getSize());
        System.out.println(pets.length);
        FileUtil.saveFile(portrait, "static/upload/");
        for (MultipartFile pet : pets) {
            FileUtil.saveFile(pet, "static/upload/");
        }
        return "success";
    }
}
